<!-- cosmetics classify -->
<template>
  <div v-loading="loading" class="app-container">
    <el-button type="primary" @click="dialog.visible = true">新建分类</el-button>
    <el-collapse style="margin: 20px 10px" accordion>
      <el-collapse-item v-for="(item, index) in classifyList" :key="index">
        <template slot="title">
          <span>{{ item.classify }}</span>
        </template>
        <el-tag v-for="(tag, tindex) in item.list" :key="tindex" type="primary" closable style="margin: 10px 10px 0 0" size="small" @close="tagClose(index, tindex)">{{ tag.value }}</el-tag>
        <div class="buttonGroup">
          <el-popover
            placement="bottom"
            width="230"
            trigger="click"
          >
            <el-input v-model="newTag" style="width: 160px" />
            <el-button type="text" style="float: right" @click="addContent(index)">确定</el-button>
            <el-button slot="reference" type="primary" style="margin: 10px 10px">新增</el-button>
          </el-popover>
          <el-popconfirm
            title="删除后无法恢复 确定删除该分类吗？"
            @onConfirm="delClassify(item.classify, item.content)"
          >
            <el-button slot="reference" type="danger">删除</el-button>
          </el-popconfirm>
        </div>
      </el-collapse-item>
    </el-collapse>

    <el-dialog
      title="新增分类"
      :visible.sync="dialog.visible"
      width="30%"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="form.classify" />
        </el-form-item>
        <el-form-item label="content">
          <el-input v-model="form.content" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="addClassify">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'CosmeticsClassify',
  data() {
    return {
      loading: false,
      classifyList: [],
      dialog: {
        visible: false,
        loading: true
      },
      newTag: '',
      confirmVisible: false,
      form: {
        classify: '',
        content: ''
      },
      rules: {
        classify: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        content: [{ required: true, message: '请输入content', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getClassifyList()
  },
  methods: {
    addClassify() {
      this.dialog.loading = true
      this.request({
        url: '/parameter/add-classify',
        method: 'POST',
        data: { classify: this.form.classify, content: this.form.content }
      }).then(res => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.getClassifyList()
          this.dialog.visible = false
        }
      })
    },
    getClassifyList() {
      this.loading = true
      this.request({
        url: '/parameter/classify-list',
        method: 'GET'
      }).then(res => {
        this.classifyList = res.data
      }).finally(this.loading = false)
    },
    delClassify(classify, content) {
      this.request({
        url: '/parameter/classify-del',
        method: 'POST',
        data: { classify, content }
      }).then(res => {
        if (res.code === 200) {
          this.getClassifyList()
          this.$message({
            message: 'success',
            type: 'success'
          })
        }
      })
    },
    addContent(index) {
      this.saveClassify(index)
    },
    tagClose(index, tindex) {
      const map = this.classifyList[index].list[tindex]
      this.loading = true
      this.request('/parameter/delete-classify-map', {
        method: 'POSt',
        data: { content: map.content, value: map.value }
      }).then(res => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: 'success'
          })
          this.getClassifyList()
        }
      }).finally(this.loading = false)
    },
    saveClassify(index) {
      console.log(this.classifyList[index].content)
      this.request({
        url: '/parameter/update-classify-map',
        method: 'POST',
        data: {
          content: this.classifyList[index].content,
          value: this.newTag
        }
      }).then(res => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: 'success'
          })
        }
        this.getClassifyList()
        this.newTag = ''
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.buttonGroup {
  float: right;
}
</style>
